package com.mchsdk.paysdk.activity;

import java.util.ArrayList;
import java.util.List;

import com.mchsdk.paysdk.adapter.MCMoneyRecordAdapter;
import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.bean.UserReLogin;
import com.mchsdk.paysdk.bean.UserReLogin.ReLoginCallback;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.entity.AddPtbEntity;
import com.mchsdk.paysdk.entity.AddPtbRecordEntity;
import com.mchsdk.paysdk.http.process.PTBRecordListProcess;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.ToastUtil;
import com.mchsdk.paysdk.view.xlist.XListView;
import com.mchsdk.paysdk.view.xlist.XListView.IXListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MCMoneyRecordActivity extends MCBaseActivity implements IXListViewListener {

	private final static String TAG = "MCMoneyRecordActivity";

	/**
	 * 无充值记录提示
	 */
	TextView txtRecordTip;

	/**
	 * 标题
	 */
//	LinearLayout llRedordTitle;

	TextView txtAccount;
	TextView txtTotal;

	private final int PAGE_START_INDEX = 1;
	private int currentIndex;

	/**
	 * 充值记录列表
	 */
	private List<AddPtbEntity> packList = new ArrayList<AddPtbEntity>();

	private XListView xListView;// 显示数据
	private MCMoneyRecordAdapter recordAdapter;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
				case Constant.RECORD_LIST_SUCCESS :
					AddPtbRecordEntity addPtbRecordEntity = (AddPtbRecordEntity) msg.obj;
					handlerRecordList(addPtbRecordEntity);
					break;
				case Constant.RECORD_LIST_FAIL :
					if(currentIndex == PAGE_START_INDEX){
						if(null != xListView){
							xListView.stopRefresh();
						}
						String tipStr = (String) msg.obj;
						if (TextUtils.isEmpty(tipStr)) {
							tipStr = "暂无充值记录";
						}
						Toast.makeText(MCMoneyRecordActivity.this, tipStr, Toast.LENGTH_SHORT).show();
						txtRecordTip.setVisibility(View.VISIBLE);
						xListView.setVisibility(View.GONE);
//					    llRedordTitle.setVisibility(View.GONE);
					}else {
						if(null != xListView){
							xListView.stopLoadMore();
						}
						ToastUtil.show(MCMoneyRecordActivity.this, "暂无更多充值记录！");
					}

					break;
				default:
					break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayout("activity_mch_money_record"));

		initView();
		initData();
	}

	/**
	 * 刷新充值记录列表
	 * 
	 * @param addPtbRecordEntity
	 */
	protected void handlerRecordList(AddPtbRecordEntity addPtbRecordEntity) {
		if(currentIndex == PAGE_START_INDEX && null == addPtbRecordEntity.getAddPtbList()){
			if(null != xListView){
				xListView.stopRefresh();
			}
			txtRecordTip.setVisibility(View.VISIBLE);
			xListView.setVisibility(View.GONE);
//			llRedordTitle.setVisibility(View.GONE);
			return;
		}

		if(null != xListView){
			if(currentIndex == PAGE_START_INDEX){
				xListView.stopRefresh();
			}else {
				xListView.stopLoadMore();
			}

		}
		if (addPtbRecordEntity.getAddPtbList() != null &&
				addPtbRecordEntity.getAddPtbList().size() > 0) {
			MCLog.e(TAG, "fun#handlerRecordList  size = " + addPtbRecordEntity.getAddPtbList().size());
			packList.addAll(addPtbRecordEntity.getAddPtbList());
			if(null != recordAdapter){
				if(currentIndex == PAGE_START_INDEX){
					recordAdapter.notifyDataSetInvalidated();
				}else {
					recordAdapter.notifyDataSetChanged();
				}
			}

			StringBuilder totalStr = new StringBuilder();
			totalStr.append("共").append(addPtbRecordEntity.getTotal()).append("条记录");
			txtTotal.setText(totalStr.toString());
		} else {
			ToastUtil.show(MCMoneyRecordActivity.this, "暂无更多充值记录！");
		}
	}

	/**
	 * 获取充值记录
	 */
	private void initData() {
		currentIndex = PAGE_START_INDEX;
		if (TextUtils.isEmpty(PersonalCenterModel.getInstance().getUserId())) {
			txtRecordTip.setVisibility(View.GONE);
			xListView.setVisibility(View.GONE);
//			llRedordTitle.setVisibility(View.GONE);

			UserReLogin reLogin = new UserReLogin(MCMoneyRecordActivity.this);
			reLogin.userToLogin(new ReLoginCallback() {

				@Override
				public void reLoginResult(boolean res) {
					if (res) {
						showRecodeList();
					} else {
						Toast.makeText(MCMoneyRecordActivity.this, "获取充值记录失败,请登录", Toast.LENGTH_SHORT).show();
						finish();
					}
				}
			});
			return;
		}
		showRecodeList();
	}

	private void showRecodeList() {
		StringBuilder accountTxt = new StringBuilder();
		accountTxt.append("账号 ").append(PersonalCenterModel.getInstance().getAccount());
		txtAccount.setText(accountTxt.toString());
		MCLog.e(TAG, "accountTxt:" + accountTxt.toString());

		getPtbRecodeList();
	}

	private void getPtbRecodeList(){
		PTBRecordListProcess ptbRecordList = new PTBRecordListProcess();
		ptbRecordList.setIndex(currentIndex);
		ptbRecordList.post(mHandler);
	}

	private void initView() {
		TextView txtTitle = (TextView) findViewById(getId("tv_mch_header_title"));
		txtTitle.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
		txtTitle.setText("充值记录");
		ImageView ivBack = (ImageView) findViewById(getId("iv_mch_header_back"));
		ivBack.setVisibility(View.VISIBLE);
		ivBack.setOnClickListener(backClick);
		ImageView ivClose = (ImageView) findViewById(getId("iv_mch_header_close"));
		ivClose.setVisibility(View.GONE);

//		llRedordTitle = (LinearLayout) findViewById(getId("ll_mch_redord_title"));
//		llRedordTitle.setVisibility(View.VISIBLE);
		txtAccount = (TextView) findViewById(getId("txt_mch_redord_account"));
		txtTotal = (TextView) findViewById(getId("txt_mch_total"));

		txtRecordTip = (TextView) findViewById(getId("txt_mch_redord_tip"));
		txtRecordTip.setVisibility(View.GONE);

		xListView = (XListView) findViewById(getId("xlistview_mch_record"));
		xListView.setVisibility(View.VISIBLE);
		recordAdapter = new MCMoneyRecordAdapter(MCMoneyRecordActivity.this, packList);
		xListView.setAdapter(recordAdapter);

		xListView.setPullLoadEnable(true);
		xListView.setPullRefreshEnable(true);
		xListView.setXListViewListener(this);
		xListView.pullRefreshing();
		xListView.setDividerHeight(0);
	}

	OnClickListener backClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	@Override
	public void onRefresh() {
		currentIndex = PAGE_START_INDEX;
		packList.clear();
		getPtbRecodeList();
	}

	@Override
	public void onLoadMore() {
		currentIndex++;
		getPtbRecodeList();
	}
}
